Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.XPath
Imports System.Xml.Xsl

Public Class InformationResponse
    Inherits Response

#Region "shared"

    Private Shared ReadOnly INFOLIST_DOC_TYPE As New Dtd("infolist.dtd", "information-list")
    Private Shared ReadOnly XPATH_INFORMATION As XPathExpression = XPathExpression.Compile("/information-list/information")
    Private Shared ReadOnly XPATH_INFO_TYPE As XPathExpression = XPathExpression.Compile("@type")
    Private Shared ReadOnly XPATH_IDENTIFIER As XPathExpression = XPathExpression.Compile("identifier")
    Private Shared ReadOnly XPATH_VERSION As XPathExpression = XPathExpression.Compile("version")
    Private Shared ReadOnly XPATH_DESCRIPTION As XPathExpression = XPathExpression.Compile("description")
    Private Shared ReadOnly XPATH_DESC_LANG As XPathExpression = XPathExpression.Compile("description/@internationalization")
    Private Shared ReadOnly XPATH_CHECK_VALUE As XPathExpression = XPathExpression.Compile("check-value")
    Private Shared ReadOnly XPATH_CHECK_VALUE_TYPE As XPathExpression = XPathExpression.Compile("check-value/@type")
    'Private Shared ReadOnly XPATH_KEY1 As XPathExpression = XPathExpression.Compile("option/item[@name = 'key 1']")
    'Private Shared ReadOnly XPATH_KEY2 As XPathExpression = XPathExpression.Compile("option/item[@name = 'key 2']")
    'Private Shared ReadOnly XPATH_KEYS As XPathExpression = XPathExpression.Compile("option/item[start-with(@name, 'key ')]")
    Private Shared ReadOnly XPATH_KEYS As New List(Of XPathExpression)
    'Private Shared ReadOnly FILELIST_STYLE_SHEET As New Xsl("filelist.xsl")

    Private Shared Function Read(ByVal xr As XmlReader) As List(Of InstallInformation)

        Dim infoList As New List(Of InstallInformation)

        Dim xd As New XPathDocument(xr)
        Dim xn As XPathNavigator = xd.CreateNavigator()

        Dim ns As XPathNodeIterator = xn.Select(XPATH_INFORMATION)
        While ns.MoveNext()
            With ns.Current
                Dim tool As String = .SelectSingleNode(XPATH_INFO_TYPE).Value
                Dim id As String = .SelectSingleNode(XPATH_IDENTIFIER).Value
                Dim ver As String = .SelectSingleNode(XPATH_VERSION).Value
                Dim desc As String = .SelectSingleNode(XPATH_DESCRIPTION).Value
                Dim desclang As String = .SelectSingleNode(XPATH_DESC_LANG).Value
                Dim chkval As String = .SelectSingleNode(XPATH_CHECK_VALUE).Value
                Dim cvtype As String = .SelectSingleNode(XPATH_CHECK_VALUE_TYPE).Value
                Dim keys As New List(Of String)
                Dim index As Integer = 1
                Do
                    Dim key_xpath As XPathExpression
                    If index <= XPATH_KEYS.Count Then
                        key_xpath = XPATH_KEYS(index - 1)
                    Else
                        key_xpath = XPathExpression.Compile("option/item[@name = 'key " & index & "']")
                        XPATH_KEYS.Add(key_xpath)
                    End If

                    Dim key_xn As XPathNavigator = .SelectSingleNode(key_xpath)
                    If key_xn Is Nothing Then
                        Exit Do
                    End If

                    Dim key_value As String = key_xn.Value
                    keys.Add(key_value)

                    index += 1
                Loop
                Dim installInfo As New InstallInformation(tool, id, ver, desc, desclang, chkval, cvtype, keys)
                infoList.Add(installInfo)
            End With
        End While

        Return infoList

    End Function
#End Region

#Region "instance"

    'Private _SessionId As String
    'Private _InfoList As String '()
    Private _InfoList As List(Of InstallInformation)

    Public Sub New(ByVal xml As String)

        MyBase.New(Net.HttpStatusCode.OK)

        'Dim xmlWithoutDtd As String = XmlString.RemoveDocType(xml)
        'INFOLIST_DOC_TYPE.Validate(xmlWithoutDtd)

        'Dim ms As New MemoryStream()
        'Try
        '    Dim tr As New StringReader(xmlWithoutDtd)
        '    Dim reader As XmlReader = XmlReader.Create(tr)

        '    ms = FILELIST_STYLE_SHEET.Transform(reader, Nothing)
        'Catch ex As Exception
        '    Throw ex
        'End Try

        '_InfoList = New StreamReader(ms).ReadToEnd() '.Split(vbNewLine)

        'Dim xr As XmlReader = XmlString.GetXmlReader(xmlWithoutDtd)
        Dim xr As XmlReader = INFOLIST_DOC_TYPE.GetValidatingReader(xml)
        _InfoList = Read(xr)

    End Sub

    Public Sub New(ByVal input As Stream)

        MyBase.New(Net.HttpStatusCode.OK)

        Dim xr As XmlReader = INFOLIST_DOC_TYPE.GetValidatingReader(input)
        _InfoList = Read(xr)

    End Sub

    'Public ReadOnly Property InfoList() As String '()
    '    Get
    '        Return _InfoList
    '    End Get
    'End Property
    Public ReadOnly Property InfoList() As ReadOnlyCollection(Of InstallInformation)
        Get
            Return New ReadOnlyCollection(Of InstallInformation)(_InfoList)
        End Get
    End Property
#End Region
End Class
